<?php
abstract class Staticizer implements IConfig_Set
{
      protected $ContentDirectory='';
      
      public function  __construct()
      {
            $this->SetConfig();
      }    

      abstract public function Build($param=NULL);

      final protected function Destroy($file)
      {
            $path=$this->ContentDirectory.'/'.$file;
            return @unlink($path);
      }
      
      final protected function Save($file,$content,$isGroup=FALSE)
      {
            #文件完整路径
            $path=join('/',explode('/',str_replace('\\','/',$this->ContentDirectory.'/'.$file)));            
          
            #文件路径-去掉文件部分
            $temp=explode('/',$path);
            $path_folder=join('/',array_slice($temp,0,count($temp)-1));
            $fileName=str_replace($path_folder.'/','',$path);
            
            #处理路径
            if(!is_dir($path_folder)){

                throw new Exception('class.Staticizer出错：不允许在文件夹没有创建之前生成静态文件。文件夹路径：'.$path_folder);
            }

            #当采用分组保存生成文件时，进行处理
            if($isGroup)
            {
                $folderName=Folder::Group($path_folder,500);
                $fileName=$folderName.'/'.$fileName;
                $path=$path_folder.'/'.$fileName;       
            }
            
            #保存文件
            $fp=fopen($path,'w');
            {
                fwrite($fp,$content);
                fclose($fp);
            }
            $fp=NULL;
            
            return $fileName;         
      }

      final public function SetConfig()
      {
            if(class_exists('Config'))
            {
                  if(method_exists('Config','Staticize'))
                  {
                        foreach(Config::Staticize() as $option=>$value){
                              $this->$option=$value;
                        }
                  }
                  else{
                        throw new Exception('class.Staticizer.SetConfig：Config配置类中不存在Staticize配置方法。');
                  }
            }
            else{
                  throw new Exception('class.Staticizer.SetConfig：Config配置类不存在。');
            }
      }      
}
?>
